package com.just521.xinshoutong.xml;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;



public class CommonXmlParser {

	public  String XML_TAG_ITEM = "root";
	static Pattern mpattern_xmlBuf = Pattern.compile(".+</root>",Pattern.DOTALL);

	public  String XML_TAG_SUB_ITEM = "rec";
	public  String XML_TAG_List = "list";
	
	HashMap<String,Object> mnodes = null;
	ArrayList<HashMap<String,Object>> mlist = null;
	String mCurTagName=null;
	String mCurTagValue=null;
	HashMap<String,Object> mCurSubNode = null;
	
	public HashMap<String,Object> getParseResult(){
		return mnodes;
	}
	public  void parse(String buffer) throws XmlPullParserException, IOException
	{
		//创建解析器,设置文件流
		String bf1 =   buffer.replace("&", "&amp;");
		
        Matcher m = mpattern_xmlBuf.matcher(bf1); 
        String bf = null;
        if ( m != null && m.find()){
        	bf = m.group(0);
        }else{
        	XmlPullParserException e = new XmlPullParserException("not support xml");
        	throw e;
        }
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
		XmlPullParser  parser = factory.newPullParser();
		parser.setInput(new StringReader(bf));
		
		//获得第一个事件,循环解析文档,直到结束
		int eventType=parser.getEventType();
        while(eventType!=XmlPullParser.END_DOCUMENT)
		{
		    switch (eventType) {
			
            //开始解析文档
			case XmlPullParser.START_DOCUMENT: 
                init_list();
                break;
				
			//开始解析元素	
            case XmlPullParser.START_TAG: 
            	mCurTagName=parser.getName();
				if( XML_TAG_ITEM.equals(mCurTagName) ){
					new_node();					
				}else if(XML_TAG_SUB_ITEM.equals(mCurTagName)){
					new_subnode();
				}
				else
				{
//					mCurTagValue = parser.getText();
					
				}
				mCurTagValue=null;	
                break;
            case XmlPullParser.TEXT:
            	mCurTagValue = parser.getText();
            	break;
                
			//解析元素结束	
            case XmlPullParser.END_TAG:
            	String tag_name=parser.getName();
				if( XML_TAG_ITEM.equals(tag_name)){
									
				}else if( XML_TAG_SUB_ITEM.equals(tag_name)){
					end_subnode();
				}else{
					end_tag();	
				}

                break;
			
			default:
				break;
            }
			
			//解析的下一个节点
            eventType=parser.next();
			
		}
	}
	
	
	
	public  void  	init_list(){}
//	public  void	change_item_name(String name){ XML_TAG_ITEM = name;}
	public void  	new_node(){
		mnodes = new HashMap<String, Object>();
		
	}
	public void   	start_tag(String  tag_name, String tag_value){}
	public void   	end_tag(){
		if(mCurSubNode!=null){
			mCurSubNode.put(mCurTagName, mCurTagValue);
		}else{
			mnodes.put(mCurTagName, mCurTagValue);
		}
	}
	public void   	new_subnode(){
		if(mlist==null){
			mlist = new ArrayList<HashMap<String,Object>>();
			mnodes.put(XML_TAG_List, mlist);
			
		}
		mCurSubNode=new HashMap<String, Object>();
	}
	public void   	end_subnode(){
		if(mlist!=null){
			mlist.add(mCurSubNode);
		}
		mCurSubNode=null;
	}

	
};

